/*
 * nwpu/2011/2011_3.c
 *
 * Harry Wei <harryxiyou@gmail.com> (C) 2013
 */

/*
 * Test data is like this.
 *
 * 3
 * 2 4 7
 * 6 7 1
 * 8 3 9
 * 4
 * 1 2 3 4
 * 9 8 2 1
 * 7 6 5 3
 * 9 7 6 3
 * 0
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define FILE_NAME		"D:\\test\\a.txt"
#define BUF				(100)

int exchange(int **matrix, int row, int _row, int counter) {
	int temp = 0;
	int col = 0;

	for (col = 0; col < counter; col++) {
		temp = matrix[row][col];
		matrix[row][col] = matrix[_row][col];
		matrix[_row][col] = temp;
	}
	return 0;
}

int sort_matrix(int **matrix, int counter) {
	int row = 0;
	int col = 0;
	int _row = 0;
//	int _col = 0;
	int __row = 0;
	int flag = 0;
	int temp = 0;

	printf("enter func sort_matrix\n");
	for (row = 0; row < counter - 1; row++) {
		for (col = 0; col < counter - 1; col++) {
			printf("%d ", matrix[row][col]);
		}
		printf("\n");
	}

	for (col = 0; col < counter - 1; col++) {
		for (row = col; row < counter - 1; row++) {
			if (0 == flag) {
				flag = 1;
				temp = matrix[row][col];
				_row = row;
			} else {
				if (matrix[row][col] > temp) {
					_row = row;
				}
				if (row == counter - 2) {
					printf("999 row is %d\n", row);
					flag = 0;
					printf("_row is %d, __row is %d, counter - 1 is %d\n", _row, __row, counter - 1);
					exchange(matrix, _row, __row, counter - 1);
					__row += 1;
				}
			}
		}
	}

	printf("After sort--------------------\n");
	for (row = 0; row < counter - 1; row++) {
		for (col = 0; col < counter - 1; col++) {
			printf("%d ", matrix[row][col]);
		}
		printf("\n");
	}
	printf("------------------------------\n");
	printf("leave func sort_matrix\n");
	return 0;
}

int free_jobs(int **matrix, int counter) {
	int row = 0;

	printf("enter func free_jobs.\n");
	printf("counter is %d.\n", counter);
#if 1
	for (row = 0; row < counter - 1; row++) {
		free(matrix[row]);
	}
//	free(matrix);
#endif
	printf("leave func free_jobs.\n");
	return 0;
}

int main(int argc, char *argv[]) {
	int ret = 0;
	FILE *fp = NULL;
	char ch = 0;
	int **matrix = NULL;
	int flag = 0;
	int counter = 0;
	int col = 0;
	int row = 0;
	int _counter = 0;

	fp = fopen(FILE_NAME, "rt");
	if (NULL == fp) {
		fprintf(stderr, "%s\n", "Fopen file error!");
		return -1;
	}
	while (!feof(fp)) {
		if (0 == flag) {
			flag = 1;
			fscanf(fp, "%c", &ch);
			counter = atoi(&ch);
			if (0 == counter) {
				ret = 0;
				goto out;
			}
			_counter = counter + 1;
			counter += 1; /*Add first line*/
			matrix = (int **) malloc(sizeof(int *) * (counter - 1));
			if (NULL == matrix) {
				fprintf(stderr, "%s\n", "Allocate error!");
				ret = -1;
				goto out;
			}
			for (row = 0; row < counter; row++) {
				matrix[row] = (int *) malloc(sizeof(int) * (counter - 1));
				if (NULL == matrix[row]) {
					fprintf(stderr, "%s\n", "Allocate error!");
					ret = -1;
					goto out;
				}
			}
			for (row = 0; row < counter - 1; row++) {
				for (col = 0; col < counter - 1; col++) {
					matrix[row][col] = 0;
				}
			}
			row = 0;
			col = 0;
		} else {
			fscanf(fp, "%c", &ch);
			if ('\n' == ch) {
				printf("999 debug\n");
				counter -= 1;
			}
			if (counter == 0) {
				flag = 0;
				sort_matrix(matrix, _counter);
				free_jobs(matrix, _counter);
			}
			if (ch >= '0' && ch <= '9') {
				//printf("99%c ", ch);
				if (col == (_counter - 1)) {
					row += 1;
					col = 0;
				}
				matrix[row][col] = atoi(&ch);
				col += 1;
			}
		}
//		printf("%c", ch);
	}
out:
	fclose(fp);
	return ret;
}